const { getPool } = require('../config/db')
const bcrypt = require('bcryptjs')

class Model {
  static async createUser(name, type, thumbnail, creator_id, industry_tag, style_tag, is_public) {
    const mysqlPool = await getPool()
    const hashedPassword = await bcrypt.hash(password, 10)
    const [rows] = await mysqlPool.execute('INSERT INTO templates (name,type,thumbnail,creator_id,industry_tag,style_tag,is_public) VALUES (?,?,?,?,?,?,?)', [name, type, thumbnail, creator_id, industry_tag, style_tag, is_public])
    return rows.insertId
  }
  static async update(name, type, thumbnail, creator_id, industry_tag, style_tag, is_public, id) {
    // console.log(username, email, phone, nickname, role, role_id, id)
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('UPDATE templates SET name=?, type=?, thumbnail=?, creator_id=?, industry_tag=?, style_tag=?, is_public=? WHERE id =?', [name, type, thumbnail, creator_id, industry_tag, style_tag, is_public, id])

    return rows.insertId
  }

  static async deleteById(id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('DELETE FROM templates WHERE id = ?', [id])

    return rows
  }

  static async findById(id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM templates WHERE id =?', [id])
    return rows[0]
  }

  static async findAll() {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM templates', [])
    return rows
  }
}

module.exports = Model
